package ferp.android.layouts;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RemoteViews;
import ferp.android.GUI;

/**
 * User: igorgok
 * Date: 1/1/14 3:32 PM
 */
@RemoteViews.RemoteView
public class VerticalLayout extends LayoutBase
{
  public VerticalLayout(Context context)
  {
    super(context);
  }

  public VerticalLayout(Context context, AttributeSet attrs)
  {
    super(context, attrs);
  }

  public VerticalLayout(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);
  }

  protected void onMeasureUnspecified(int widthMeasureSpec, int heightMeasureSpec)
  {
    int count = getChildCount(), visible = 0;
    int mw = 0, height = 0;

    for (int i = 0; i < count; ++i)
    {
      View child = getChildAt(i);

      if (child.getVisibility() != GONE)
      {
        ++visible;

        child.measure(widthMeasureSpec, heightMeasureSpec);

        MarginLayoutParams mlp = (MarginLayoutParams)child.getLayoutParams();
        int cw = child.getMeasuredWidth(), ch = child.getMeasuredHeight();

        //Log.debug(Log.TAG, "VL child #" + visible + " (" + child.getClass().getSimpleName() + ") " + cw + 'x' + ch + ", margins " + mlp.leftMargin + '/' + mlp.topMargin + '/' + mlp.bottomMargin + '/' + mlp.rightMargin);

        mw = Math.max(mw, mlp.leftMargin + cw + mlp.rightMargin);

        height += mlp.topMargin + child.getMeasuredHeight() + mlp.bottomMargin;
      }
    }

    //Log.debug(Log.TAG, "VL unspecified total " + mw + 'x' + height);

    setMeasuredDimension(mw, height);
  }

  protected void onMeasureExactly(int sx, int sy)
  {
    int count = getChildCount(), visible = 0;
    int height = 0;

    for (int i = 0; i < count; ++i)
    {
      View child = getChildAt(i);

      if (child.getVisibility() != GONE)
      {
        ++visible;

        MarginLayoutParams mlp = (MarginLayoutParams)child.getLayoutParams();
        // child margins and dimensions are percentage of our dimensions
        int mt = sy * mlp.topMargin / 100, mb = sy * mlp.bottomMargin / 100,
            w;

        switch (mlp.width)
        {
          case LayoutParams.WRAP_CONTENT:
            w = MeasureSpec.UNSPECIFIED;
            break;

          case LayoutParams.MATCH_PARENT:
            w = MeasureSpec.makeMeasureSpec(sx, MeasureSpec.EXACTLY);
            break;

          default:
            w = MeasureSpec.makeMeasureSpec(sx * mlp.width / 100, MeasureSpec.EXACTLY);
            break;
        }

        // set the actual child dimensions
        child.measure(w, mlp.height == LayoutParams.WRAP_CONTENT ? MeasureSpec.UNSPECIFIED : MeasureSpec.makeMeasureSpec(sy * mlp.height / 100, MeasureSpec.EXACTLY));

        //Log.debug(Log.TAG, "VL child #" + visible + " (" + child.getClass().getSimpleName() + ") " + child.getMeasuredWidth() + 'x' + child.getMeasuredHeight() + ", vertical margins " + mt + '/' + mb);

        height += mt + child.getMeasuredHeight() + mb;
      }
    }

    // starting offset
    offset = (sy - height) / 2;

    //Log.debug(Log.TAG, "VL exact total " + sx + 'x' + sy + ", offset " + offset);

    setMeasuredDimension(sx, sy);
  }

  @Override
  protected void onLayout(boolean b, int left, int top, int right, int bottom)
  {
    int count = getChildCount(), visible = 0;
    int h = bottom - top, y = offset; // + top;

    //Log.debug(Log.TAG, "VL layout " + left + ',' + top + " - " + right + ',' + bottom);

    for (int i = 0; i < count; ++i)
    {
      View child = getChildAt(i);

      if (child.getVisibility() != GONE)
      {
        ++visible;

        MarginLayoutParams mlp = (MarginLayoutParams)child.getLayoutParams();
        // calculate child top and bottom margins again
        int mt = h * mlp.topMargin / 100, mb = h * mlp.bottomMargin / 100;
        int cw = child.getMeasuredWidth(), ch = child.getMeasuredHeight(), cl = GUI.center(cw, left, right) - left, ct = y + mt, cb = ct + ch;

        child.layout(cl, ct, cl + cw, cb);

        // next vertical position
        y = cb + mb;

        //Log.debug(Log.TAG, "VL child #" + visible + " (" + child.getClass().getSimpleName() + ") " + child.getLeft() + ',' + child.getTop() + " - " + child.getRight() + ',' + child.getBottom());
      }
    }
  }

  private int offset;
}
